package qeug.shsh.bpm.Factory;

import qeug.shsh.bpm.annotation.ThridSrevice;
import qeug.shsh.bpm.service.BPMSrevice;
import qeug.shsh.bpm.service.BPMSreviceV2;
import qeug.shsh.bpm.service.IG6Service;
import qeug.shsh.bpm.service.SAPService;
import qeug.shsh.bpm.service.impl.BPMServiceImpl;
import qeug.shsh.bpm.service.impl.BPMServiceImplV2;
import qeug.shsh.bpm.service.impl.G6ServiceImpl;
import qeug.shsh.bpm.service.impl.SAPServicelmpl;

/**
 * @projectName: qeug-cosmic-root-project
 * @package: qeug.shsh.bpm.Factory
 * @className: BPMSreviceFactory
 * @author: wenyx
 * @description: TODO
 * @date: 2023/10/24 18:24
 * @version: 1.0
 */
// 使用注解的工厂类
public class ThirdServiceFactory {
    private static <T> T createService(Class<T> serviceType) throws Exception {
        Class<?>[] classes = new Class[]{BPMServiceImpl.class, G6ServiceImpl.class, SAPServicelmpl.class, BPMServiceImplV2.class}; // 此处需要根据实际情况提供所有可能的实现类
        for (Class<?> clazz : classes) {
            if (clazz.isAnnotationPresent(ThridSrevice.class)) {
                ThridSrevice annotation = clazz.getAnnotation(ThridSrevice.class);
                String value = annotation.value();
                if (value != null && !value.isEmpty() && value.equals(clazz.getSimpleName()) && serviceType.isAssignableFrom(clazz)) {
                    return serviceType.cast(clazz.getDeclaredConstructor().newInstance());
                }
            }
        }
        throw new Exception("Invalid " + serviceType.getSimpleName() + " type.");
    }

    public static BPMSrevice createBPMSrevice() throws Exception {
        return createService(BPMSrevice.class);
    }
    public static BPMSreviceV2 createBPMV2Srevice() throws Exception {
        return createService(BPMSreviceV2.class);
    }

    public static IG6Service createG6Service() throws Exception {
        return createService(IG6Service.class);
    }
    public static SAPService createSAPService() throws Exception {
        return createService(SAPService.class);
    }

}
